# -*- coding: utf-8 -*-
from geraldo import Report, landscape, ReportBand, ObjectValue, SystemField,\
     FIELD_ACTION_COUNT, FIELD_ACTION_SUM, BAND_WIDTH, Label, ReportGroup, SubReport, Line

from reportlab.lib.units import cm
from reportlab.lib.pagesizes import A5
from reportlab.lib.enums import TA_CENTER, TA_RIGHT
from reportlab.lib.colors import navy, red

class RelatorioAluno(Report):
    title = 'Listagem de alunos'
    print_if_empty = True
    page_size = landscape(A5)
    
    class band_page_header(ReportBand):
        height = 1.3*cm
        elements = [
                SystemField(expression='%(report_title)s', top=0.1*cm, left=0, width=BAND_WIDTH,
                    style={'fontName': 'Helvetica-Bold', 'fontSize': 14, 'alignment': TA_CENTER}),
                Label(text="Matricula",     top=0.8*cm, left=0.5*cm ),
                Label(text=u'Nome',         top=0.8*cm, left=3*cm),
                Label(text=u'Telefone',     top=0.8*cm, left=8*cm),
                Label(text=u'Cpf',          top=0.8*cm, left=11*cm),
                Label(text=u'Email',        top=0.8*cm, left=14*cm),
                SystemField(expression='Página %(page_number)d de %(page_count)d', top=0.1*cm,
                            width=BAND_WIDTH, style={'alignment': TA_RIGHT}),
                ]
        borders = {'bottom': True}
    
    class band_detail(ReportBand):
        height = 0.5*cm
        elements=(
                ObjectValue(attribute_name='id',        left=0.5*cm,
                  get_value=lambda instance: str(instance.id).format('07d')),
                ObjectValue(attribute_name='nome',      left=3*cm, width=12*cm,),
                ObjectValue(attribute_name='telefone',  left=8*cm,),
                ObjectValue(attribute_name='cpf',       left=11*cm,),
                ObjectValue(attribute_name='email',     left=14*cm,),                   
                )

    class band_page_footer(ReportBand):
        height = 0.5*cm
        elements = [
                        Label(text='Geraldo Reports', top=0.1*cm),
                        SystemField(expression=u'Impressão em %(now:Y, b d)s às %(now:H:i)s', top=0.1*cm,
                        width=BAND_WIDTH, style={'alignment': TA_RIGHT}),
                    ]
        borders = {'top': True}

class RelatorioAdmin(Report):
    title = 'Listagem de administradores'
    print_if_empty = True
    page_size = landscape(A5)

    class band_page_header(ReportBand):
        height = 1.3*cm
        elements = [
                SystemField(expression='%(report_title)s', top=0.1*cm, left=0, width=BAND_WIDTH,
                    style={'fontName': 'Helvetica-Bold', 'fontSize': 14, 'alignment': TA_CENTER}),
                Label(text=u'Nome',         top=0.8*cm, left=1*cm),
                Label(text=u'Telefone',     top=0.8*cm, left=6*cm),
                Label(text=u'Cpf',          top=0.8*cm, left=9*cm),
                Label(text=u'Email',        top=0.8*cm, left=12*cm),
                SystemField(expression='Página %(page_number)d de %(page_count)d', top=0.1*cm,
                            width=BAND_WIDTH, style={'alignment': TA_RIGHT}),
                ]
        borders = {'bottom': True}
    
    class band_detail(ReportBand):
        height = 0.5*cm
        elements=(
                ObjectValue(attribute_name='nome',      left=1*cm, width=12*cm,),
                ObjectValue(attribute_name='telefone',  left=6*cm,),
                ObjectValue(attribute_name='cpf',       left=9*cm,),
                ObjectValue(attribute_name='email',     left=12*cm,),                   
                )

    class band_page_footer(ReportBand):
        height = 0.5*cm
        elements = [
                        Label(text='Geraldo Reports', top=0.1*cm),
                        SystemField(expression=u'Impressão em %(now:Y, b d)s às %(now:H:i)s', top=0.1*cm,
                        width=BAND_WIDTH, style={'alignment': TA_RIGHT}),
                    ]
        borders = {'top': True}

class RelatorioProfessor(Report):
    title = 'Listagem de professores'
    print_if_empty = True
    page_size = landscape(A5)

    class band_page_header(ReportBand):
        height = 1.3*cm
        elements = [
                SystemField(expression='%(report_title)s', top=0.1*cm, left=0, width=BAND_WIDTH,
                    style={'fontName': 'Helvetica-Bold', 'fontSize': 14, 'alignment': TA_CENTER}),
                Label(text=u'Nome',         top=0.8*cm, left=1*cm),
                Label(text=u'Telefone',     top=0.8*cm, left=6*cm),
                Label(text=u'Cpf',          top=0.8*cm, left=9*cm),
                Label(text=u'Email',        top=0.8*cm, left=12*cm),
                SystemField(expression='Página %(page_number)d de %(page_count)d', top=0.1*cm,
                            width=BAND_WIDTH, style={'alignment': TA_RIGHT}),
                ]
        borders = {'bottom': True}
    
    class band_detail(ReportBand):
        height = 0.5*cm
        elements=(
                ObjectValue(attribute_name='nome',      left=1*cm, width=12*cm,),
                ObjectValue(attribute_name='telefone',  left=6*cm,),
                ObjectValue(attribute_name='cpf',       left=9*cm,),
                ObjectValue(attribute_name='email',     left=12*cm,),                   
                )

    class band_page_footer(ReportBand):
        height = 0.5*cm
        elements = [
                        Label(text='Geraldo Reports', top=0.1*cm),
                        SystemField(expression=u'Impressão em %(now:Y, b d)s às %(now:H:i)s', top=0.1*cm,
                        width=BAND_WIDTH, style={'alignment': TA_RIGHT}),
                    ]
        borders = {'top': True}


class Rel_Aproveitamento(Report):
    title = 'Aproveitamento do Aluno'
    page_size = landscape(A5)
    print_if_empty = True
    
    class band_summary(ReportBand):
        height = 1.8*cm
    
    class band_page_header(ReportBand):
        height = 0.8*cm
        elements = [
            SystemField(expression='%(report_title)s', top=0.1*cm, left=0, width=BAND_WIDTH,
                style={'fontName': 'Helvetica-Bold', 'fontSize': 14, 'alignment': TA_CENTER}),
            SystemField(expression='Página # %(page_number)d de %(page_count)d', top=0.1*cm,
                width=BAND_WIDTH, style={'alignment': TA_RIGHT}),
        ]
    
    class band_page_footer(ReportBand):
        height = 0.5*cm
        elements = [
            SystemField(expression=u'Impressão em %(now:%Y, %b %d)s às %(now:%H:%M)s', top=0.1*cm,
                width=BAND_WIDTH, style={'alignment': TA_RIGHT}),
        ]
        
    class band_detail(ReportBand):
        height = 1.5*cm
        elements = [
            Label(text="Nome:",                 top=0*cm, left=0*cm,\
                  style={'fontName': 'Helvetica-Bold', 'fontSize': 14}, width=9.5*cm),

            ObjectValue(attribute_name='nome',  top=0*cm, left=2.2*cm,\
                        style={'fontName': 'Helvetica', 'fontSize': 14}, width=15.0*cm ),
            ]
    subreports = [
        
        SubReport(
            queryset_string = '%(object)s.nota_set.all().order_by(u"curso")',
            band_header = ReportBand(
                    height=1*cm,
                    elements=[                       
                        Label(text='Curso',             top=0.4*cm, left=0*cm,
                              style={'fontName': 'Helvetica-Bold', 'fontSize': 14}),\
                        Label(text='Descrição',  top=0.4*cm, left=4.5*cm,
                              style={'fontName': 'Helvetica-Bold', 'fontSize': 14}),\
                        Label(text='Total',        top=0.4*cm, left=9*cm,
                              style={'fontName': 'Helvetica-Bold', 'fontSize': 14}),\
                        Label(text='Nota',              top=0.4*cm, left=12.5*cm,
                              style={'fontName': 'Helvetica-Bold', 'fontSize': 14}),\
                       ],
                    borders={'bottom': True},
                ),
            band_detail = ReportBand(
                    height=1.5*cm,
                    elements=[
                        ObjectValue(attribute_name='curso',               top=0, left=0*cm,\
                                    style={'fontName': 'Helvetica-Bold', 'fontSize': 14}),
                        ObjectValue(attribute_name='descricao',          top=0, left=4.5*cm,\
                                    style={'fontName': 'Helvetica-Bold', 'fontSize': 14}, ),
                        ObjectValue(attribute_name='notaTotal',            top=0, left=9*cm,\
                                    style={'fontName': 'Helvetica-Bold', 'fontSize': 14}),
                        ObjectValue(attribute_name='notaAluno',                  top=0, left=12.5*cm,\
                                    style={'fontName': 'Helvetica-Bold', 'fontSize': 14}),
                        ],
                    borders={'bottom': True},
                ),
        ),
    ]

class Rel_Historico(Report):
    title = 'Histórico do Aluno'
    page_size = landscape(A5)
    print_if_empty = True
    
    class band_page_header(ReportBand):
        height = 1.0*cm
        elements = [
            SystemField(expression='%(report_title)s', top=0.1*cm, left=0, width=BAND_WIDTH,
                style={'fontName': 'Helvetica-Bold', 'fontSize': 14, 'alignment': TA_CENTER}),
            SystemField(expression='Página # %(page_number)d de %(page_count)d', top=0.1*cm,
                width=BAND_WIDTH, style={'alignment': TA_RIGHT}),
        ]
    
    class band_detail(ReportBand):
        height = 1*cm
        elements = [
            Label(text="Nome:",                 top=0*cm, left=0.6*cm,\
                  style={'fontName': 'Helvetica-Bold', 'fontSize': 14}, width=9.5*cm),

            ObjectValue(attribute_name='nome',  top=0*cm, left=2.2*cm,\
                        style={'fontName': 'Helvetica', 'fontSize': 14}, width=15.0*cm ),
            ]
    subreports = [
        
        SubReport(
            queryset_string = '%(object)s.presenca_set.all().order_by(u"curso",u"aula")',
            band_header = ReportBand(
                    height=0.4*cm,
                    elements=[                       
                        Label(text='Curso',             top=-0.2*cm, left=0*cm,
                              style={'fontName': 'Helvetica-Bold', 'fontSize': 14}),\
                        Label(text='Aula',              top=-0.2*cm, left=4.5*cm,
                              style={'fontName': 'Helvetica-Bold', 'fontSize': 14}),\
                        Label(text='Presença',          top=-0.2*cm, left=14*cm,
                              style={'fontName': 'Helvetica-Bold', 'fontSize': 14}),\
                       ],
                    borders={'bottom': True},
                ),
            band_detail = ReportBand(
                    height=0.6*cm,
                    elements=[
                        ObjectValue(attribute_name='curso',                 top=0, left=0*cm,width=14.0*cm,\
                                    style={'fontName': 'Helvetica-Bold', 'fontSize': 14}),
                        ObjectValue(attribute_name='aula',                  top=0, left=4.5*cm,width=14.0*cm,\
                                    style={'fontName': 'Helvetica-Bold', 'fontSize': 14}, ),
                        ObjectValue(attribute_name='presenca',              top=0, left=14*cm,width=14.0*cm,\
                                    style={'fontName': 'Helvetica-Bold', 'fontSize': 14},\
                                    get_value=lambda instance:u'Concluído' if instance.presenca == True else u'Em andamento'),
                        ],
                ),
            band_footer = ReportBand(
                    height=0.5*cm,
                    borders = {'top': True},
                ),
        ),
    ]
    
    class band_page_footer(ReportBand):
        height = 0.5*cm
        elements = [
            SystemField(expression=u'Impressão em %(now:%Y, %b %d)s às %(now:%H:%M)s', top=0.1*cm,
                width=BAND_WIDTH, style={'alignment': TA_RIGHT}),
    ]
        
 
